package com.neouton.crm.pm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.PageUtil;
import com.neouton.common.util.ResponseAPI;
import com.neouton.crm.pm.entity.PurchaseOrderProductVO;
import com.neouton.crm.pm.entity.PurchaseOrderVO;
import com.neouton.crm.pm.service.IInternalPurchaseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 内部采购相关controller
 * @Author libingkun
 * @Date 2024年4月7日
 * @Version 1.0
 *          初始化迁移
 */
@RestController
@RequestMapping("internalPurchase")
public class InternalPurchaseController {

    @Resource
    private IInternalPurchaseService internalPurchaseService;

    /**
     * 保存采购信息
     * @param purchaseOrder
     * @return
     */
    @PostMapping("/saveOrUpdatePurchaseOrde")
    public ResponseAPI savePurchasesoInfo(@RequestBody PurchaseOrderVO purchaseOrder,@RequestHeader("token") String token){
        purchaseOrder.setCreator(AuthUtils.getUserId());
        purchaseOrder.setLastUpdateBy(AuthUtils.getUserId());
        return ResponseAPI.successData(internalPurchaseService.saveOrUpdatePurchaseOrder(purchaseOrder,token));
    }

    /**
     * 获取当日最大采购单号
     * @return
     */
    @PostMapping("/getPurchaseOrdeMaxNumByDate")
    public ResponseAPI getPurchaseOrdeMaxNumByDate(){
        String maxNum = internalPurchaseService.getPurchaseOrdeMaxNumByDate();
        return ResponseAPI.successData(maxNum);
    }

    /**
     * 更具采购单号获取采购信息
     * @return
     */
    @PostMapping("/getPurchaseOrdeInfoById/{purchaseOrdeId}")
    public ResponseAPI getPurchaseOrdeInfoById(@PathVariable(value = "purchaseOrdeId") int purchaseOrdeId){
        PurchaseOrderVO purchaseOrde = internalPurchaseService.getPurchaseOrdeInfoById(purchaseOrdeId);
        return ResponseAPI.successData(purchaseOrde);
    }

    /**
     * 更具采购单号获取采购信息
     * @return
     */
    @PostMapping("/getPurOrdeInfoById/{purchaseOrdeId}")
    public ResponseAPI getPurOrdeInfoById(@PathVariable(value = "purchaseOrdeId") int purchaseOrdeId){
        List<PurchaseOrderVO>  purchaseOrderVOS  = internalPurchaseService.getPurOrdeInfoById(purchaseOrdeId);
        return ResponseAPI.successData(purchaseOrderVOS);
    }
    /**
     * 采购信息驳回审批
     * @param purchaseOrder
     * @return
     */
    @PostMapping("/rejectionPurchaseOrderInfo")
    public ResponseAPI rejectionPurchaseOrderInfo(@RequestBody PurchaseOrderVO purchaseOrder,@RequestHeader("token") String token){
        return ResponseAPI.successData(internalPurchaseService.rejectionPurchaseOrderInfo(purchaseOrder, token));
    }

    /**
     * 采购信息通过审批
     * @param purchaseOrder
     * @return
     */
    @PostMapping("/approvePurchaseOrderInfo")
    public ResponseAPI approvePurchaseOrderInfo(@RequestBody PurchaseOrderVO purchaseOrder,@RequestHeader("token") String token){
        return ResponseAPI.successData(internalPurchaseService.approvePurchaseOrderInfo(purchaseOrder, token,false));
    }

    /**
     * 通过申请表主键查询现有数据
     * @param approvalId
     * @return
     */
    @PostMapping("/queryPurchaseOrderByApprovalId/{approvalId}")
    public ResponseAPI queryPurchaseOrderByApprovalId(@PathVariable(value = "approvalId") Integer approvalId){
        return ResponseAPI.successData(internalPurchaseService.queryPurchaseOrderByApprovalId(approvalId));
    }

    /**
     * 通过申请表主键查询历史
     * @param approvalId
     * @return
     */
    @PostMapping("/queryPurchaseOrderHistoryByApprovalId/{approvalId}")
    public ResponseAPI queryPurchaseOrderHistoryByApprovalId(@PathVariable(value = "approvalId") Integer approvalId){
        return ResponseAPI.successData(internalPurchaseService.queryPurchaseOrderHistoryByApprovalId(approvalId));
    }

    /**
     * 分页查新采购信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryPurchaseOrdesByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryPurchaseOrdesByPage( @PathVariable(value = "pageNum") int pageNum,
                                             @PathVariable(value = "pageSize") int pageSize,
                                             @RequestBody PurchaseOrderVO entity,
                                             @RequestHeader("token") String token) {
        IPage<PurchaseOrderVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<PurchaseOrderVO> result = internalPurchaseService.queryPurchaseOrdesByPage(page ,entity ,token);
        return ResponseAPI.successData(result);
    }

    @PostMapping("/exportPurchaseOrdes")
    public void exportPurchaseOrdes(HttpServletResponse response,
                                   @RequestBody PurchaseOrderVO entit,
                                   @RequestHeader("token") String token) throws Exception{
        internalPurchaseService.exportPurchaseOrdes(response,entit,token);
    }



    /**
     * 分页查询采购待办信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalPurByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalPurByPage( @PathVariable(value = "pageNum") int pageNum,
                                                 @PathVariable(value = "pageSize") int pageSize,
                                                 @RequestBody PurchaseOrderVO entity) {
        IPage<PurchaseOrderVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<PurchaseOrderVO> result = internalPurchaseService.queryApprovalPurByPage(page ,entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 分页查询采购已办信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalPurDoneByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalPurDoneByPage( @PathVariable(value = "pageNum") int pageNum,
                                               @PathVariable(value = "pageSize") int pageSize,
                                               @RequestBody PurchaseOrderVO entity) {
        IPage<PurchaseOrderVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<PurchaseOrderVO> result = internalPurchaseService.queryApprovalPurDoneByPage(page ,entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 分页查询采购申请记录信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalPurRecordByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalPurRecordByPage( @PathVariable(value = "pageNum") int pageNum,
                                                     @PathVariable(value = "pageSize") int pageSize,
                                                     @RequestBody PurchaseOrderVO entity,
                                                     @RequestHeader("token") String token) {
        IPage<PurchaseOrderVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<PurchaseOrderVO> result = internalPurchaseService.queryApprovalPurRecordByPage(page ,entity,token);
        return ResponseAPI.successData(result);
    }

    /**
     * 删除采购单
     * @param purchaseOrdeId
     * @return
     */
    @PostMapping("/deletePurchaseOrderById/{purchaseOrdeId}")
    public ResponseAPI deletePurchaseOrderById(@PathVariable(value = "purchaseOrdeId") int purchaseOrdeId){
        return ResponseAPI.successData(internalPurchaseService.deletePurchaseOrderById(purchaseOrdeId));
    }

    /**
     * 查询项目
     * @return
     */
    @PostMapping("/selectAllProjectByPur")
    public ResponseAPI selectAllProjectByPur(){
        return ResponseAPI.successData(internalPurchaseService.selectAllProjectByPur());
    }

}
